import { IMediator, IControllerDetails, IAppSettings, IDispatcher, IRouter, IValid, ILoader } from "./interfaces";
import { Mediator } from "./core/Mediator";
import { AppEvent } from "./event/AppEvent";
import { AppEventName } from "./Define";
import { Router } from "./core/Router";
import { Dispatcher } from "./core/Dispatcher";
import { UIManager } from "./ui/UIManager";
import { Loader } from "./core/Loader";
import { IEngine } from "./engine/IEngine";
import { res } from "./res/res";
import { LocalManager } from "./data/LocalManager";
import { ViewManager } from "./tools/ViewManager";
export class App implements IValid {
    isValid(): boolean {
        return true;
    }
    private _mediator: IMediator;
    private _dispatcher: IDispatcher;
    private _router: IRouter;
    private _uiMgr: UIManager;
    private _loader: ILoader;
    private _engine: IEngine;
    private _localMgr: LocalManager;
    private _viewMgr: ViewManager;
    private _onErrorHandler: (o: Object) => void;

    constructor(appSettings: IAppSettings) {
        this._engine = appSettings.engine;
        res.init(this._engine)
        this._mediator = new Mediator(appSettings.isDebug || false)
        this._router = new Router(this._mediator)
        this._dispatcher = new Dispatcher(this._mediator)
        this._onErrorHandler = appSettings.onErrorHandler;
        this._uiMgr = new UIManager(this._mediator, appSettings.root, appSettings.uiSettings)
        this._loader = new Loader(this._mediator)
        this._localMgr = new LocalManager(this._engine.getLocalstorage())
        this._viewMgr = new ViewManager(this._uiMgr, res.loader, this._engine)
    }

    init() {
        this._loader.init();
        this._uiMgr.init()
        this._router.init()
        this._dispatcher.init();
        let event = new AppEvent(AppEventName.app_error, (data: any) => {
            this._onErrorHandler(data)
        }, this)
        this._mediator.subscribe(event)

        this._mediator.publish(AppEventName.app_init, null)

    }

    get local() {
        return this._localMgr;
    }

    get engine(): IEngine {
        return this._engine;
    }

    get viewMgr() {
        return this._viewMgr;
    }

}